const EssayModel = require('../do/models/essay'),
    Type = require('../do/models/type'),
    sequelize = require('sequelize');
class EssayService {
    async getEssayAll() {
        return await EssayModel.findAll({
            include: {
                association:
                    EssayModel.belongsTo(Type, { foreignKey: 'typeid' })
            },
            'order': [['createdAt', 'DESC']]
        });
    }
    async getEssayUp(offset, limit) {
        return await EssayModel.findAll({
            limit: limit,
            offset: offset,
            include: {
                association:
                    EssayModel.belongsTo(Type, { foreignKey: 'typeid' })
            },
            where: { status: 1 },
            'order': [['createdAt', 'DESC']]
        }
        );
    }
    async getEssayType(offset, limit, id) {
        return await EssayModel.findAll({
            limit: limit,
            offset: offset,
            include: {
                association:
                    EssayModel.belongsTo(Type, { foreignKey: 'typeid' })
            },
            where: { typeid: id },
            'order': [['createdAt', 'DESC']]
        }
        );
    }
    async getEssay(tid, offset, limit) {
        if (!tid) {
            return await EssayModel.findAll({
                limit: limit,
                offset: offset,
                include: {
                    association:
                        EssayModel.belongsTo(Type, { foreignKey: 'typeid' })
                },
                'order': [['createdAt', 'DESC']]
            }
            );
        }
        return await EssayModel.findOne({
            where: {
                id: tid
            },
            include: {
                association:
                    EssayModel.belongsTo(Type, { foreignKey: 'typeid' })
            }
        }
        );
    }
    async getEssayCount() {
        return await EssayModel.findAll({
            attributes: [[sequelize.fn('COUNT', sequelize.col('id')), 'count']]
        })
    }
    async createEssay(data) {
        return await EssayModel.create(data);
    }
    async destroyEssay(id) {
        return await EssayModel.destroy({
            where: {
                id
            }
        })

    }
    async updateEssay(id, data) {
        let { title, content, typeid, status, intro } = data;
        return await EssayModel.update({
            title: title,
            content: content,
            typeid: typeid,
            status: status,
            intro: intro
        }, {
            where: {
                id
            }
        })
    }
    async changeStatusEssay(id, status) {
        return await EssayModel.update({
            status: status
        }, {
            where: {
                id
            }
        })
    }
    async typeisExit(typeid) {
        return await EssayModel.findAll({
            attributes: [[sequelize.fn('COUNT', sequelize.col('id')), 'count']],
            where: {
                typeid: typeid
            }
        })
    }
}
module.exports = new EssayService();